//
//  PGPictureView.m
//  youqu
//
//  Created by 彭鹏 on 16/10/16.
//  Copyright © 2016年 pengpeng. All rights reserved.
//

#import "PGPictureView.h"
#import "PGPostItem.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <DACircularProgress/DALabeledCircularProgressView.h>
#import "PGSeeBigViewController.h"
@interface PGPictureView ()
@property (weak, nonatomic) IBOutlet UIImageView *gifImgeView;
@property (weak, nonatomic) IBOutlet DALabeledCircularProgressView *processView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIImageView *placeholderImageView;
@property (weak, nonatomic) IBOutlet UIButton *seeBigImageButton;

@end





@implementation PGPictureView

- (IBAction)seeBigImage {
//    self touchesBegan:<#(nonnull NSSet<UITouch *> *)#> withEvent:<#(nullable UIEvent *)#>
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    PGSeeBigViewController *seeBigViewController = [[PGSeeBigViewController alloc] init];
    seeBigViewController.item = _item;
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:seeBigViewController animated:YES completion:nil];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    _processView.roundedCorners = 10;
    _processView.trackTintColor = [UIColor greenColor];
    _processView.progressTintColor = [UIColor redColor];
    _processView.progressLabel.textColor = [UIColor redColor];
//    _processView.progressLabel.text = @"0%%";

}

+ (instancetype)pictureView {
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
}


- (void)setItem:(PGPostItem *)item {
    _item = item;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:item.image0] placeholderImage:nil options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        CGFloat progress = 1.0*receivedSize/expectedSize;
        _processView.progress = progress;
        _processView.progressLabel.text = [NSString stringWithFormat:@"%.0f%%",progress*100];
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        item.smallImage = image;
        if (!item.is_big) return;
        //        UIGraphicsBeginImageContext(<#CGSize size#>)
        UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0);
        CGFloat w = SCREENW - 2*commonMargin;
        CGFloat h = w/item.width*item.height;
        CGFloat x = 0;
        CGFloat y = 0;
        [image drawInRect:CGRectMake(x, y, w, h)];
        
        image = UIGraphicsGetImageFromCurrentImageContext();
        
        
        UIGraphicsEndImageContext();
        
        self.imageView.image = image;
    }];
    
    _gifImgeView.hidden = !item.is_gif;
    if (item.is_big) {
        _seeBigImageButton.hidden = NO;
        _imageView.layer.masksToBounds = YES;
        _imageView.contentMode = UIViewContentModeTop;
    }else {
        _seeBigImageButton.hidden = YES;
        _imageView.layer.masksToBounds = NO;
        _imageView.contentMode = UIViewContentModeScaleToFill;
    }
    
    
    
    
}


- (IBAction)bigPictureClick:(id)sender {
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
